//
//  RegistViewController.m
//  History
//
//  Created by 七 on 16/9/6.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "RegistViewController.h"
#import "AuthCodeView.h"
@interface RegistViewController ()
{
    __strong IBOutlet AuthCodeView *authView;
    __weak IBOutlet UIButton *loginBtn;
    
    __weak IBOutlet UITextField *phoneTF;
    __weak IBOutlet UITextField *passTF;
    __weak IBOutlet UITextField *authTF;
    
    
    
}
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    loginBtn.layer.cornerRadius = 5.0;
    passTF.secureTextEntry = YES;
    
    [authView getAuthcode];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Action
- (IBAction)backPress:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)commitBtnPressed:(id)sender {
    if (phoneTF.text.length == 0) {
        [self.view makeToast:phoneTF.placeholder position:@"center"];
        return;
    }
    if (passTF.text.length == 0) {
        [self.view makeToast:passTF.placeholder position:@"center"];
        return;
    }
    if (authTF.text.length == 0) {
        [self.view makeToast:authTF.placeholder position:@"center"];
        return;
    }
    if (![[authTF.text lowercaseString] isEqualToString:[authView.authCodeStr lowercaseString]]) {
        [self.view makeToast:@"验证码不正确" position:@"center"];
        return;
    }
    
    NSDictionary *params = @{@"c_nick":@"",@"c_phone":phoneTF.text,@"c_password":passTF.text};
    [WebService registerWithParams:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            if ([result[@"errcode"] integerValue] == 0) {
                NSDictionary *customer = result[@"result"][@"customer"][@"value"];
                [UserEntity sharedUser].UserID = [customer[@"c_id"] convertToString];
                [UserEntity sharedUser].NickName = [customer[@"c_nick"] convertToString];
                [UserEntity sharedUser].MobilePhone = phoneTF.text;
                [UserEntity sharedUser].Password = passTF.text;
                [UserEntity sharedUser].loginSuccess = YES;

                [self dismissViewControllerAnimated:YES completion:^{}];
            }else{
                [self.view makeToast:result[@"errmsg"] position:@"center"];
            }
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}


//隐藏键盘
- (void)hideKeyboard
{
    [phoneTF resignFirstResponder];
    [passTF resignFirstResponder];
    [authTF resignFirstResponder];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self hideKeyboard];
}

@end
